<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 配送站设置ajax类
 * @package  ajax/backend/system
 * @author create by allen.qiang
 */


quanli_require('module/base/system/sysDistribution.class.php');


class ajax_distributionSetting {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1://配送站列表
                    $this->distributionList();
                    break;

                case 2://查询配送站
                    $this->searchPayTypeDetail();
                    break;

                case 3://查询地图区域
                    $this->searchArea();
                    break;

                case 4:	//查询运能设置
                    $this->searchSaleShow();
                    break;

                case 5: //添加运能设置
                    $this->addGoodsSku();
                    break;

                case 100:
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit','change'))) {

                        returnJsonData('参数错误');

                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->edit();//新增、修改 配送站

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteDistribution();//删除配送站

                    } else if ($_REQUEST['act'] == 'change') {

                        $this->changeSendTypeUseflg(); //配送站 启用/禁用

                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
     * 配送站列表
     */
    private function distributionList(){
        global $G_PGLOB;

        //实例化配送站设置类
        $sysDistribution = new sysDistribution( ) ;
        $arg_array = $_REQUEST;
        $result = $sysDistribution->search( $arg_array ) ;

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        if ($result) {

            $edit_permission = $G_PGLOB->has_permission(26);

            foreach($result as &$data) {

                if ($edit_permission) {

                    $operation_html = '';
                    $operation_html .= '<a href="javascript:void(0);" onclick="distribution.edit.open(' . $data['distribution_id'] . ');">修改</a>&nbsp;&nbsp;&nbsp;';
                    $operation_html .= '<a href="javascript:void(0);" onclick="distribution.editSku(' . $data['distribution_id'] . ');">运能设置</a>';
                    $use_html = '';
                    if ($data['useFlg']) {
                        $use_html .= '<a href="javascript:void(0);" onclick="distribution.change.check(' . $data['distribution_id'] . ', ' . $data['useFlg'] . ');">启用</a>';
                    } else {
                        $use_html .= '<a href="javascript:void(0);" onclick="distribution.change.check(' . $data['distribution_id'] . ', ' . $data['useFlg'] . ');">禁用</a>';
                    }

                    $result_array['rows'][] = array (
                        'id' => $data['distribution_id'],
                        'cell' => array(
                            $operation_html,
                            $data['distribution_id'],
                            $data['distribution_name'],
                            $data['distribution_code'],
                            $data['principal'],
                            $data['mobile'],
                            $data['phone'],
                            $data['address'],
                            $use_html
                        )
                    );

                } else {

                    $operation_html = '';
                    $result_array['rows'][] = array (
                        'id' => $data['distribution_id'],
                        'cell' => array(
                            $operation_html,
                            $data['distribution_id'],
                            $data['distribution_name'],
                            $data['distribution_code'],
                            $data['principal'],
                            $data['mobile'],
                            $data['phone'],
                            $data['address'],
                            $data['audio'],
                            $data['useFlg'] == 1 ? '启用' : '禁用',
                        )
                    );

                }

            }

        }


        echo json_encode($result_array);
    }

    /**
     * 新增/修改 配送站
     */
    private function edit() {

        global $G_SHOP;

        //实例配送站类
        $sysDistribution = new sysDistribution();

        //新增/修改
        $arg_array = $_REQUEST;
        $arg_array['creator'] = $G_SHOP->currentUserID;
        $result_array = $sysDistribution->editDistribution( $arg_array );

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 查询地图区域
     */
    private function searchArea() {

        if (!isset($_REQUEST['cityID']) || !quanli_is_int($_REQUEST['cityID'])) {
            returnJsonData('参数错误');
        }
        quanli_require('module/base/system/mapAreaZoning.class.php');

        //实例地图区域划分 公共方法类
        $mapAreaZoning = new mapAreaZoning();

        $arg_array = array(
            'type' => 1,
            'cityID' => $_REQUEST['cityID']
        );

        $distribution_array = $mapAreaZoning->searchMapArea( $arg_array );

        if ($distribution_array) {
            returnJsonData('ok', 200, $distribution_array);
        } else {
            returnJsonData('地图区域划分信息没有找到', 404);
        }

    }

    /**
     * 配送站启用/禁用
     */
    private function changeSendTypeUseflg () {

        //实例配送站类
        $sysDistribution = new sysDistribution();

        //配送站 启用/禁用
        $result_array = $sysDistribution->changeDistributionTechnicianUseflg( $_REQUEST );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

    /**
     * 删除配送站
     */
    private function deleteDistribution () {

        //实例配送站类
        $sysDistribution = new sysDistribution();

        //批量删除配送站
        $result_array = $sysDistribution->deleteDistribution( $_REQUEST['p_array'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

    /**
     * 查询运能设置方式
     */
    public function searchSaleShow(){

        global $G_SHOP;

        quanli_include('module/base/system/sysMax.class.php');

        //实例配送运能设置类
        $sysMax = new sysMax();

        $_REQUEST['creator'] = $G_SHOP->currentUserID;

        $result_array = $sysMax->searchSaleShow( $_REQUEST ) ;

        echo json_encode($result_array);
    }

    /**
     * 添加运能设置
     *
     */
    public function addGoodsSku(){

        global $G_SHOP;

        quanli_include('module/base/system/sysMax.class.php');


        //实例配送运能设置类
        $sysMax = new sysMax();

        $_REQUEST['creator'] = $G_SHOP->currentUserID;

        $result_array = $sysMax->editDistributionSend( $_REQUEST );


        echo json_encode($result_array);

    }

}